<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: Attribute.php 2870 2010-05-17 03:04:13Z huuphuoc $
 * @since        2.0.5
 */

class Shop_Models_Dao_Pdo_Mysql_Value extends Tomato_Model_Dao
    implements Shop_Models_Interface_Value
{
    public function convert($entity)
    {
        return new Shop_Models_Value($entity);
    }

    public function add($value_type)
    {
        $this->_conn->insert($this->_prefix.'shop_value_type', array(
                'value_type_name'   => $value_type->value_type_name,
                'value_type_icon'   => $value_type->value_type_icon,
                'tags'              => $value_type->tags,
                'value_type_costs'  => $value_type->value_type_costs,
                'value_type_price'  => $value_type->value_type_price,
            ));
        return $this->_conn->lastInsertId($this->_prefix.'shop_value_type');
    }

    public function update($value_type)
    {
        $where[] = 'value_type_id = '.$this->_conn->quote($value_type->value_type_id);
        $this->_conn->update($this->_prefix.'shop_value_type', array(
                'value_type_name'   => $value_type->value_type_name,
                'value_type_icon'   => $value_type->value_type_icon,
                'tags'              => $value_type->tags,
                'value_type_costs'  => $value_type->value_type_costs,
                'value_type_price'  => $value_type->value_type_price,
            ), $where);
    }

    public function addContent($value_content)
    {
        $this->_conn->insert($this->_prefix.'shop_value_content', array(
                'value_type_id'             => $value_content->value_type_id,
                'value_content_name'        => $value_content->value_content_name,
                'value_content_description' => $value_content->value_content_description,
                'language_code'             => $value_content->language_code,
            ));
        return $this->_conn->lastInsertId($this->_prefix.'shop_value_content');
    }

    public function updateContent($value_content)
    {
        $where[] = 'value_content_id = '.$this->_conn->quote($value_content->value_content_id);
        $this->_conn->update($this->_prefix.'shop_value_content', array(
                'value_type_id'             => $value_content->value_type_id,
                'value_content_name'        => $value_content->value_content_name,
                'value_content_description' => $value_content->value_content_description,
                'language_code'             => $value_content->language_code,
            ), $where);
    }

    public function getById($id){
        $row = $this->_conn
                    ->select()
                    ->from(array('v' => $this->_prefix . 'shop_value_type'))
                    ->where('v.value_type_id = ?', $id)
                    ->limit(1)
                    ->query()
                    ->fetch();
        return (null == $row) ? null : new Shop_Models_Value($row);
    }

    public function getValueContentById($id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('vc' => $this->_prefix.'shop_value_content'))
                        ->where('vc.value_type_id = ?', $id)
                        ;
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function delete($conditions){
        $p_id = $conditions['product_id'];
        $a_id = $conditions['attribute_type_id'];
        $v_id = $conditions['value_type_id'];

        $sql = "UPDATE `shop_product_attribute` SET
                `value_type_id_chain` = REPLACE(`value_type_id_chain`, ',{$v_id},', ',')
                WHERE `product_id` = {$p_id} AND attribute_type_id = {$a_id};";

        $void = $this->_conn->query($sql);
        if($void){
            $void = $this->deleteValue($v_id);
            if($void){
                $this->deleteValueCotent($v_id);
            }
        }
    }

    public function deleteValue($id){
        $where[] = 'value_type_id = '.$this->_conn->quote($id);
        return $this->_conn->delete($this->_prefix.'shop_value_type', $where);
    }

    public function deleteValueContent($id){
        $where[] = 'value_type_id = '.$this->_conn->quote($id);
        return $this->_conn->delete($this->_prefix.'shop_value_content', $where);
    }

    public function countValue($id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('od' => $this->_prefix.'shop_order_detail'), array('num_value' => 'COUNT(*)'))
                        ->where(" od.product_value_type_id_chain like '%:\"{$id}\",%' or od.product_value_type_id_chain like '%:{$id},%' ")
                        ;
        $row = $select->query()->fetch();
        return $row->num_value;
    }

    public function countValue1($id)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('cd' => $this->_prefix.'shopping_cart_detail'), array('num_value' => 'COUNT(*)'))
                        ->where(" cd.product_attribute_value_id_chain like '%:\"{$id}\",%' or cd.product_attribute_value_id_chain like '%:{$id},%' ")
                        ;
        $row = $select->query()->fetch();
        return $row->num_value;
    }

    /**=========================Front end===========================*/

    public function values($str){
        $select = $this->_conn
                        ->select()
                        ->from(array('vt' => $this->_prefix.'shop_value_type'), array('value_type_id', 'value_type_name', 'value_type_icon', 'value_type_costs', 'value_type_price'))
                        ->joinLeft(array('vc' => $this->_prefix.'shop_value_content'), "vt.value_type_id = vc.value_type_id AND vc.language_code = '{$this->_lang}'", array('value_content_id', 'value_content_name', 'value_content_description', 'language_code'))
                        ->where("'{$str}' like concat('%',vt.value_type_id,'%')")
                        ;
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function getFEById($id){
        $row = $this->_conn
                    ->select()
                    ->from(array('vt' => $this->_prefix . 'shop_value_type'), array('value_type_id', 'value_type_name', 'value_type_icon', 'value_type_costs', 'value_type_price'))
                    ->joinLeft(array('vc' => $this->_prefix . 'shop_value_content'), "vt.value_type_id = vc.value_type_id AND vc.language_code = '{$this->_lang}'", array('value_content_id', 'value_content_name', 'value_content_description', 'language_code'))
                    ->where('vt.value_type_id = ?', $id)
                    ->limit(1)
                    ->query()
                    ->fetch();
        return (null == $row) ? null : new Shop_Models_Value($row);
    }

    public function search($keyword){
        $select = $this->_conn
                        ->select()
                        ->from(array('vt' => $this->_prefix.'shop_value_type'), array('value_type_id'))
                        ->joinLeft(array('vc' => $this->_prefix.'shop_value_content'), "vt.value_type_id = vc.value_type_id AND vc.language_code = '{$this->_lang}'", array())
                        ->where("vt.value_type_name like '%{$keyword}%' OR vc.value_content_name like '%{$keyword}%' OR vc.value_content_description like '%{$keyword}%'")
                        ;
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }
}
